﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace WebSystem
{
    public partial class MTea : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {




        }

        protected void btnOut_Click(object sender, EventArgs e)
        {
            String strCn = "Server=pc.hstc.cn;database=dhd2014115121;uid=2014115121;pwd=19960404";
            SqlConnection cn = new SqlConnection(strCn); //建对象
            try
            {
                cn.Open();//打开连接
                string strTno = this.txtTno.Text;
                //忽略检测范围
                string strSql = "SELECT   Tno,Tname,Dno FROM      Teacher  WHERE   (Tno = N'" + strTno + "')";
                //string strSql = "select * from Student";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    this.txtTname.Text = dr["Tname"].ToString();
                
                    
                    this.txtDno.Text = dr["Dno"].ToString();
                    
                }
                else
                {
                    this.lblInfo.Text = "查询失败，没有此教师！";
                }
                //this.GridView1.DataSource = dr;
                //this.GridView1.DataBind();
                //dr.Close();
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;//将异常信息显示在label中
            }
            finally
            {
                cn.Close();
            }
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] == "1")
            {
                Page.Theme = "1";
            }
            else if (Session["Theme"] == "3")
            {
                Page.Theme = "3";
            }

            else
            {
                Page.Theme = "2";
            }
        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014115121;uid=2014115121;pwd=19960404";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {

                cn.Open();

                string strSql = "DELETE    FROM  Teacher   WHERE  Tno = N'" + this.txtTno.Text + "'";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.lblInfo.Text = "删除数据失败！";
                }
                else
                {
                    this.lblInfo.Text = "删除数据成功！";
                }



            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            
            txtDno.Text = "";
            
            txtTname.Text = "";
            txtTno.Text = "";
            lblInfo.Text = "";
        }

        protected void btnUpd_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014115121;uid=2014115121;pwd=19960404";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                //string strSql = "UPDATE  Student SET Name = N'计算机学院', Age = 20 WHERE   (No = N'2014112101')";
                string strSql = "UPDATE  Teacher SET       Tname = N'" + this.txtTname.Text + "',Dno = " + this.txtDno.Text + " WHERE   (Tno = N'" + this.txtTno.Text + "')";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.lblInfo.Text = "更新失败！";
                }
                else
                {
                    this.lblInfo.Text = "更新" + iUpd.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014115121;uid=2014115121;pwd=19960404";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                string strSql = "INSERT INTO Teacher (Tno,Tname,Dno) VALUES   (N'" + this.txtTno.Text + "', N'" + this.txtTname.Text + "'," + this.txtDno.Text + ")";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.lblInfo.Text = "新增失败，此教师已存在！";
                }
                else
                {
                    this.lblInfo.Text = "新增" + iUpd.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("MTeaNew.aspx");
        }
    }
}